package edu.kit.ksri.db.mturk.plugins.correctness;

import org.apache.log4j.Logger;

import com.amazonaws.mturk.requester.Assignment;

import edu.kit.ksri.db.mturk.utils.messaging.Result;
import edu.kit.ksri.db.mturk.utils.messaging.ResultOutputCollector;
import edu.kit.ksri.db.mturk.var.Icd;
import edu.kit.ksri.db.mturk.var.StaticInformation;
import edu.kit.ksri.db.mturk.workerpools.AbstractWorker;
import edu.kit.ksri.db.mturk.workerpools.ICDWorker;
import edu.kit.ksri.db.mturk.workerpools.PoolManager;
import edu.kit.ksri.db.mturk.workerpools.StandardWorker;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public class SimInputQuality extends AbstractQualityControlImplementation {

	private static Logger basiclog = Logger.getLogger("basic." + SimInputQuality.class);

	@Override
	protected void onMessage(Result result) {
		// FIXME can we cope with more than one assignment within the same
		// result message?
		for (Assignment a : result.getAssignments()) {
			initSimInputQuality(a);
		}
	}

	private void initSimInputQuality(Assignment assignment) {
		AbstractWorker w = PoolManager.getInstance().getPoolForName("ACTIVE")
				.getWorkerForID(assignment.getWorkerId());
		
		if (w == null) {
			// work has been moved to another pool in the mean time, check
			// whether we can find him in blocked or pending
			w = PoolManager.getInstance().getPoolForName("PENDING")
					.getWorkerForID(assignment.getWorkerId());
			if (w == null)
				w = PoolManager.getInstance().getPoolForName("BLOCKED")
						.getWorkerForID(assignment.getWorkerId());
			if (w == null) {
				// was also not found in another pool what shall we do? create a
				// new one? throw an exception? FIXME

				// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
				// w = new Worker(assignment.getWorkerId());
				// Assume correctness level of 75% instead of 100%
				if(StaticInformation.scenario instanceof Icd){
					w = new ICDWorker(assignment.getWorkerId(),0.75);
				}
				else{
					// ROB_APRIL09 >>
					//w = new StandardWorker(assignment.getWorkerId(),0.75);
					w = new StandardWorker(assignment.getWorkerId(),20,19,1);
					// ROB_APRIL09 <<
				}
				// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

				PoolManager.getInstance().getPoolForName("ACTIVE").addWorker(w);				
			}
		}
		Result r = ResultOutputCollector.getInstance().remove(
				assignment.getHITId());
		if (r != null)
			r = Result.mergeResults(r, new Result(assignment.getHITId(),
					assignment));
		else
			r = new Result(assignment.getHITId(), assignment);
		ResultOutputCollector.getInstance().put(assignment.getHITId(), r);
	}

}
